package com.dcrzl.gazgmh.common.aspect;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dcrzl.gazgmh.common.entity.sys.SysLog;
import com.dcrzl.gazgmh.common.utils.HttpContextUtils;
import com.dcrzl.gazgmh.common.utils.IPUtils;
import com.dcrzl.gazgmh.common.utils.Result;
import com.dcrzl.gazgmh.common.utils.UserInfoByTokenUtils;
import com.dcrzl.gazgmh.module.sys.service.SysLogService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * 系统日志，切面处理类
 */
@Slf4j
@Aspect
@Component

public class SysLogAspect {
    @Autowired
    private SysLogService sysLogService;

    @Pointcut("@annotation(com.dcrzl.gazgmh.common.annotation.SysLog)")
    public void logPointCut() {

    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        //执行方法

        //执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;
        //保存日志
        return saveSysLog(point, time);
    }

    private Object saveSysLog(ProceedingJoinPoint joinPoint, long time) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Object result = joinPoint.proceed();
        SysLog sysLog = new SysLog();
        com.dcrzl.gazgmh.common.annotation.SysLog syslog = method.getAnnotation(com.dcrzl.gazgmh.common.annotation.SysLog.class);
        //注解上的描述
        sysLog.setOperation(syslog.value());
        if (syslog.type() == 0) {
            sysLog.setType(0);
            Object proceed = joinPoint.proceed();
            Result loginResult = (Result) proceed;
            if (loginResult.isSuccess()&&loginResult.getCode()==0) {
                sysLog.setLoginStatus(0);
            } else {
                sysLog.setLoginStatus(1);
            }
            sysLog.setLoginMsg(loginResult.getMsg());
            Object[] args = joinPoint.getArgs();
            String jsonString = JSONObject.toJSONString(args[0]);
            JSONObject jsonObject = JSONObject.parseObject(jsonString);
            sysLog.setLoginName(jsonObject.getString("loginName"));
        }else{
            sysLog.setType(1);
            //请求的参数
            Object[] args = joinPoint.getArgs();
            try {
                String params = JSON.toJSONString(args);
                sysLog.setParams(params);
            } catch (Exception e) {

            }
        }

        //请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        sysLog.setMethod(className + "." + methodName + "()");



        //获取request
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        //设置IP地址
        sysLog.setIp(IPUtils.getIpAddr(request));

        //用户名
        JSONObject jsonObject = UserInfoByTokenUtils.parseJson(request);
        String username = jsonObject.getString("username");
        log.info(username);
        sysLog.setUsername(username);


        sysLog.setTime(time);
        sysLog.setCreateTime(new Date());
        //保存系统日志
        sysLogService.insert(sysLog);
        return result;
    }
}
